const Runtime = require('../runtime/index')
const SessionStore = Runtime.SessionStore
const config = Runtime.App.AppConfig.app

let topo = {
    iconSize: 50,
    hmargin: 400,
    vmargin: 200,
    hpadding: 100,
    vpadding: 200,
    startX: 0,
    startY: 0,
    userList: [],
    serviceList: [],
    dbList: [],
    rpcList: [],
	/**
	 * @returns
	 */
	getTopoDataAsync (opts) {
		let url = '/agent/sys/topology'
		let httpClient = new Runtime.App.HttpClient({
			httpHost: config.host,
			urlPath: url,
			formData: {
				'systemId': opts.systemId,
                'interval': opts.interval
			}
		})
		return httpClient.post()
	},

    getTopoData (opts) {
        return this.getTopoDataAsync(opts)
            .then( result => {
                topo.fixData(result.data)
                return result
            })
    },

    fixData: function(result) {
        this.userList = []
        this.serviceList = []
        this.dbList = []
        this.rpcList = []
        if(!result || !result.nodes){
            result = {}
            result.nodes = []
            result.links = []
            return
        }
        for(var i=0; i<result.nodes.length; i++){
            var node = result.nodes[i]
            switch(node.type){
                case 'user':
                    this.userList.push(node);
                    node.symbol = 'image://static/images/user.png'
                    break
                case 'SERVER':
                    this.serviceList.push(node)
                    node.symbol = 'image://static/images/java.png'
                    break
                case 'DATABASE':
                    this.dbList.push(node)
                    // 显示samllType,弹框显示name,因此这里需要更改原name值
                    node.name2 = node.name
                    node.name = node.smallType
                    node.symbol = 'image://static/images/mysql.png'
                    break
                case 'RPC':
                    this.rpcList.push(node)
                    node.symbol = 'image://static/images/rpc.png'
                    break
            }
        }
        for(var i=0; i<result.links.length; i++){
            var link = result.links[i]
            var node = this.findNodeById(result, link.source)
            var label = link.count+'cpm, '+link.times+'ms'
            link.source = this.findNodeIndexById(result, link.source)
            link.target = this.findNodeIndexById(result, link.target)
            if(link.source === link.target){
              var arr = node.symbol.split('.')
              node.name = node.name + '\r\n(' + label + ')'
              if(arr.length == 2){
                node.symbol = arr[0] + '_loop.' + arr[1]
              }
            }
            if(link.source===undefined || link.target===undefined || link.source === link.target){
              result.links.splice(i, 1)
              i--
              continue
            }
            link.label = label
        }
        this.locate(result)
    },

    locate () {
        this.locateUser()
        this.locateService()
        this.locateDB()
        this.locateRPC()
    },

    locateUser () {
        var startX = this.startX
        this.userList.forEach(function(element, i) {
          element.x = startX
          element.y = i * (this.vpadding + this.iconSize)
        }, this)
      },

      locateService () {
        var startX = this.startX + this.hmargin
        this.serviceList.forEach(function(element, i) {
          element.x = startX + (i % 2) * (this.hpadding + this.iconSize)
          element.y = i * (this.vpadding + this.iconSize)
        }, this)
      },

      locateDB () {
        var startX = this.startX + 2 * this.hmargin + this.serviceList.length * (this.hpadding + this.iconSize)
        this.dbList.forEach(function(element, i) {
          element.x = startX
          element.y = i * (this.vpadding + this.iconSize)
        }, this)
      },

      locateRPC () {
        var maxIndex = this.serviceList.length //this.userList.length > this.serviceList.length ? this.userList.length : this.serviceList.length
        //maxIndex = maxIndex > this.dbList.length ? maxIndex : this.dbList.length
        var startY = this.vmargin + maxIndex * this.iconSize + (maxIndex-1) * this.vpadding
        this.rpcList.forEach(function(element, i) {
          element.x = i * (this.hpadding + this.iconSize) + this.hpadding
          element.y = startY          
        }, this)
      },

      findNodeIndexById (result, id) {
        for(var i=0; i<result.nodes.length; i++){
            var node = result.nodes[i]
            if(node.id == id){
                return i
            }
        }
      },

      findNodeById (result, id) {
        for(var i=0; i<result.nodes.length; i++){
            var node = result.nodes[i]
            if(node.id == id){
                return node
            }
        }
      }
}

module.exports = topo